package netease.b20210828.t1;

import java.util.*;

/**
 * <li style="color: red;">Prob</li>
 *
 * <li style="color: red;">Desc</li>
 *
 * <li style="color: red;">Link</li>
 *
 * @author habitplus
 * @since 2021-08-28 15:42
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int[][] arr = new int[n][2];
            Map<Integer, List<Integer>> map = new HashMap<>();

            int k = 0, tt;
            for (int i = 0; i < n; i++) {
                arr[i][0] = sc.nextInt();
                k = sc.nextInt();
                arr[i][1] = k;

                if (k > 0) {
                    for (int j = 0; j < k; j++) {
                        tt = sc.nextInt();
                        if (map.containsKey(tt - 1)) {
                            map.get(tt - 1).add(i);
                        } else {
                            List<Integer> tmpList = new ArrayList<>();
                            tmpList.add(i);
                            map.put(tt - 1, tmpList);
                        }
                    }
                }
            }

            System.out.println(handle(arr, map));
        }
    }

    private static int handle(int[][] arr, Map<Integer, List<Integer>> map) {
        if (arr == null || arr.length == 0) {
            return 0;
        }
        int n = arr.length;
        boolean[] v = new boolean[n];
        int ans = 0;

        while (true) {
            List<Integer> task = new ArrayList<>();

            // 取可以执行的任务的最小值并记录可以执行的任务
            int minDay = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                if (v[i] || arr[i][1] != 0) continue;

                task.add(i);
                minDay = Math.min(minDay, arr[i][0]);
            }

            // 所有任务都执行完毕
            if (task.size() == 0) {
                break;
            }

            ans += minDay;

            // 更新执行时间
            for (int k : task) {
                arr[k][0] -= minDay;
                if (arr[k][0] == minDay) {
                    // 当前任务已执行完
                    v[k] = true;

                    // 更新以当前任务为前置任务的任务的入度
                    List<Integer> list = map.getOrDefault(k, new ArrayList<>());
                    for (int e : list) { arr[e][1]--; }
                }
            }
        }

        return ans;
    }
}
